package com.itheima.app.manager;


import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.MovementScore;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//专门发消息的MQ
@Component //加入到容器中
public class MQMovementManager {


    //mqMovementManager.sendMovementMQ(UserHolder.getUserId() ,movement.getId() , MQMovementManager.MOVEMENT_LIKE);
    //需要在各个不同的位置上 加上 保存得分的状态代码
    //针对动态的操作  标志
    public static final Integer MOVEMENT_PUBLISH = 1;// 发动态
    public static final Integer MOVEMENT_BROWSE = 2;// 浏览动态
    public static final Integer MOVEMENT_LIKE = 3;// 点赞
    public static final Integer MOVEMENT_LOVE = 4;// 喜欢
    public static final Integer MOVEMENT_COMMENT = 5;// 评论
    public static final Integer MOVEMENT_DISLIKE = 6;// 取消点赞
    public static final Integer MOVEMENT_DISLOVE = 7;// 取消喜欢

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @DubboReference
    private MovementService movementService;
    /**
     * @param userId  谁
     * @param publishId  哪个动态
     * @param type  什么操作
     */
    //发送消息的代码
    public void sendMovementMQ(Long userId , ObjectId publishId , Integer type){
        //1.创建一个操作的得分对象
        MovementScore movementScore = new MovementScore();
        //  xxx 对 什么 xxx动态 进行了 xxx操作
        movementScore.setUserId(userId);
        Movement movement = movementService.findById(publishId);
        //由于大数据平台不支持string类型的id 所以必须要转换
        //pid其实 我们自己为每一个动态赋值的唯一标识
        movementScore.setMovementId( movement.getPid() );
        movementScore.setDate(System.currentTimeMillis());//时间


        //得分
        switch (type) {
            case 1: {
                movementScore.setScore(20d);
                break;
            }
            case 2: {
                movementScore.setScore(1d);
                break;
            }
            case 3: {
                movementScore.setScore(5d);
                break;
            }
            case 4: {
                movementScore.setScore(8d);
                break;
            }
            case 5: {
                movementScore.setScore(10d);
                break;
            }
            case 6: {
                movementScore.setScore(-5d);
                break;
            }
            case 7: {
                movementScore.setScore(-8d);
                break;
            }
        }

        //2.发送给 tanhua-recommend端进行保存对象
        rabbitTemplate.convertAndSend("tanhua.recommend.movement" , movementScore);
    }

}
